package top.jnaw.test.controller;

import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import java.util.ArrayList;
import java.util.List;
import lzj.tools.Https;
import top.jnaw.test.model.Permissions;
import top.jnaw.test.model.Roles;
import top.jnaw.test.model.RolesPermissions;
import top.jnaw.test.service.RoleService;

public class RoleController extends Controller {

  //--------- 一般角色权限都是定死的  -----------

  /**
   * 添加角色
   */
  public void addRole() {
    String rolename = getPara("role", "").trim();
    String cnname = getPara("cnname", "").trim();
    Roles role = new Roles();
    boolean res = false;
    String msg = "";
    if (StrKit.notBlank(rolename)) {
      Roles roles = Roles.dao.findById(rolename);
      if (roles != null) {
        msg += "该角色已存在！";
      } else {
//        Date date = new Date();
//        String roleString = date.getTime() + "";
        role.setRole(rolename);
        role.setRoleCnName(cnname);
        res = role.save();
      }
    }
    if (res) {
      renderJson(Https.success("添加成功", ""));
    } else {
      renderJson(Https.failure("添加失败！" + msg, ""));
    }
  }

  /**
   * 删除角色
   */
  public void rmRole() {
    String role = getPara("role", "").trim();
    Roles roles = Roles.dao.findById(role);
    if (roles.delete()) {
      renderJson(Https.success("删除成功", ""));
    } else {
      renderJson(Https.failure("删除失败", ""));
    }
  }

  /**
   * 角色列表
   */
  public void ListRole() {
    String cnname = getPara("cnname", "");
    List<Roles> list = RoleService.getRoleList(cnname);
    renderJson(Https.success("查询成功", list));
  }


  /**
   * 所有权限列表
   */
  public void ListPer() {
    String name = getPara("name", "");
    List<Permissions> list = RoleService.getPermissionsList(name);
    renderJson(Https.success("查询成功", list));
  }
  /**
   * 角色拥有的权限列表
   */
  public void ListPerByRole() {
    String role = getPara("role", "");
    List<RolesPermissions> list = RoleService.getListByRole(role);
    renderJson(Https.success("查询成功", list));
  }

  /**
   * 给角色附权限。
   */
  public void roletoper() {
    String role = getPara("role", "").trim();
    String per = getPara("pers", "").trim();
    String msg = "";
    boolean res = false;
    if (StrKit.notBlank(role)) {
      String[] aa = per.split(",");
      List<RolesPermissions> listPer = new ArrayList<>();
      for (int a = 0; a < aa.length; a++) {
        RolesPermissions rolesPermissions = new RolesPermissions();
        rolesPermissions.setRoleName(role);
        rolesPermissions.setPermission(aa[a]);
        listPer.add(rolesPermissions);
      }
      res = RoleService.delByRolename(role);
      if (listPer.size() > 0) {
        int[] result = Db.batchSave(listPer, 10);
        res = true;
      }
    } else {
      msg += "角色key不能为空！";
    }
    if (res) {
      renderJson(Https.success("赋权成功", ""));
    } else {
      renderJson(Https.failure("赋权失败", ""));

    }
  }

  /**
   * 修改角色，修改角色名
   */
  public void set() {
    String role = getPara("role", "");//角色英文名
    String cnname = getPara("cnname", "").trim();
    boolean res = false;
    String msg = "";
    if (StrKit.notBlank(role, cnname)) {
      Roles roles = Roles.dao.findById(role);
      if (roles != null) {
        roles.setRoleCnName(cnname);
        res = roles.update();
      } else {
        msg += "该角色不存在！";
      }
    } else {
      msg += "角色中文名或角色key不能为空！";
    }
    if (res) {
      renderJson(Https.success("修改成功", ""));
    } else {
      renderJson(Https.failure("修改失败！" + msg, ""));

    }
  }

}
